local DIY = require "packages/diy_utility/diy_utility"

local shejing = fk.CreateSkill{
  name = "steam__shejing",
  tags = {DIY.ReadySkill},
}

shejing:addEffect("active", {
  card_num = 0,
  target_num = 0,
  prompt = "#steam__shejing",
  can_use = function(self, player)
    if player:hasSkill(shejing.name) then
      for g, general in pairs(Fk.generals) do
        if not table.contains(Fk:currentRoom().disabled_generals, g) and not general.hidden and not general.total_hidden then
          for _, skill in ipairs(general:getSkillNameList(true)) do
            if Fk.skills[skill]:hasTag(DIY.ReadySkill) and not player:hasSkill(skill, true) then
              return true
            end
          end
        end
      end
    end
  end,
  on_use = function(self, room, use)
    local player = use.from
    local skels = {}
    for g, general in pairs(Fk.generals) do
      if not table.contains(Fk:currentRoom().disabled_generals, g) and not general.hidden and not general.total_hidden then
        for _, skill in ipairs(general:getSkillNameList(true)) do
          if Fk.skills[skill]:hasTag(DIY.ReadySkill) and not player:hasSkill(skill, true) then
            table.insert(skels, skill)
          end
        end
      end
    end
    if #skels == 0 then return end
    local exist_skills = player:getTableMark("@steam__shejing")
    local pool = table.random(skels, 3)
    local choice, choice2
    choice = room:askToCustomDialog(player, {
      skill_name = shejing.name,
      qml_path = "packages/utility/qml/ChooseSkillBox.qml",
      extra_data = { table.random(skels, 3), 1, 1, "#steam__shejing-choose", {} },
    })
    if choice == "" then
      choice = table.random(pool)
    else
      choice = choice[1]
    end
    table.insert(exist_skills, choice)

    local skillnum = #exist_skills
    if skillnum > 1 then
      -- table.removeOne(exist_skills, choice)
      -- choice2 = room:askToCustomDialog(player, {
      --   skill_name = shejing.name,
      --   qml_path = "packages/utility/qml/ChooseSkillBox.qml",
      --   extra_data = { exist_skills, skillnum - 2, skillnum - 2, "#steam__shejing-discard:::" .. skillnum - 2, {} },
      -- })
      -- if choice2 == "" then
      --   choice2 = table.random(exist_skills)
      -- else
      --   choice2 = choice2[1]
      -- end
      -- table.removeOne(exist_skills, choice2)
      choice2 = table.remove(exist_skills, 1)
    end
    room:setPlayerMark(player, "@steam__shejing", exist_skills)
    room:handleAddLoseSkills(player, choice)
    DIY.setReadySkill(player, choice, shejing.name, true)
    if choice2 then room:handleAddLoseSkills(player, "-"..choice2) end
  end
})

Fk:loadTranslationTable{
  ["steam__shejing"] = "涉经",
  [":steam__shejing"] = "<a href='diy_ready_skill'>蓄势技</a>，出牌阶段，你可以观看随机三个武将牌上的蓄势技并获得其中一个技能（若拥有以此法获得的技能则替换之），以此法获得的蓄势技视为蓄势中。",

  ["#steam__shejing"] = "涉经：观看随机三个蓄势技并获得其中一个",

  ["#steam__shejing-choose"] = "涉经：请获得一个蓄势技",
  ["#steam__shejing-discard"] = "涉经：请移除%arg个蓄势技",

  ["@steam__shejing"] = "涉经",

  ["$steam__shejing1"] = "日诵三卷书，学向勤中得。",
  ["$steam__shejing2"] = "勤学而笃行，明辨且慎思。",
}

return shejing 